/**
 *******************************************************************************
 * @brief       Provide a brief description of the module here (Single line).
 * 
 * @author      Ashwin Vijayakumar
 * 
 * @file        TimeWp.c
 * 
 * @version
 * 
 * @date        Aug 18, 2012 10:20:27 PM
 * 
 * @details     Provide a detailed description of the module here (Multi-line).
 * 
 * @pre         Mention any preconditions required to use this module.
 * 
 * @bug         List any bugs in this module.
 * 
 * @note        List any odd modes while using this module.
 * 
 * @warning     List any warnings against using the objects of this module.
 *******************************************************************************
 * @copyright 2012
 * <a href="http://www.afflatustech.com">Afflatus Techtronics</a>
 * and <a href="http://www.ashwinvijayakumar.co.nr">Ashwin Vijayakumar</a>
 * @n@n
 * This work is licensed under the Creative Commons
 * Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy
 * of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or
 * send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain
 * View, California, 94041, USA.
 *******************************************************************************
 */

/**@cond -----------------------------------------------------------------------
 * Includes section
 * Add all #include here.
 * ----------------------------------------------------------------- @endcond */

/**@cond System headers -------------------------------------------- @endcond */
#include <stdio.h>
#include <stdlib.h>

/**@cond Local headers --------------------------------------------- @endcond */
#include "TimeWp.h"
#include "McalUart.h"
#include "PalDs1629.h"
#include "PrintWp.h"
#include "strtol.h"

/**@cond -----------------------------------------------------------------------
 * typedef section (File scope)
 * Add typedef's with file scope here.
 * Capitalization: Upper_Camel_Case.
 * Declare typedef's with project scope in TimeWp.h.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Constants section (File scope)
 * Add all constants with file scope here.
 * Capitalization: ALL_CAPS.
 * Note that all constants listed here are limited to this file scope.
 * For project wide scope, declare constants in TimeWp.h.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Macros section (File scope)
 * Add all macros with file scope here.
 * Capitalization: ALL_CAPS.
 * Note that all macros listed here are limited to this file scope.
 * For project wide scope, declare macros in TimeWp.h.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Global variables section (File scope)
 * Add global variables with file scope here. 
 * Capitalization: lower_case.
 * Declare them with 'static' keyword.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Global variables section (Project scope)
 * PROJECT SCOPE G-VARIABLES ARE LIKE FORBIDDEN FRUIT. REFRAIN FROM USING THEM, 
 * DECLARE FILE SCOPE G-VARIABLES INSTEAD AND DEFINE GETTER/SETTER METHODS TO 
 * ACCESS THEM.
 * Add global variables with project scope here. 
 * Capitalization: lower_case.
 * Declare them without 'static' keyword.
 * Also declare these variables in TimeWp.h with 'extern' keyword.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Function prototypes section (File scope)
 * Add prototypes for all functions with file scope here. 
 * Capitalization: Upper_Camel_Case.
 * Declare them with 'static' keyword.
 * For functions with project scope, declare the function prototype in 
 * TimeWp.h without 'static' keyword.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Getter/setter method section (Project scope)
 * Define getter/setter methods to provide project scope global access to file 
 * scope global variables. (AVOID USING THESE IF YOU HAVE THROUGHPUT ISSUES).
 * Capitalization: get_Camel_Case/is_Camel_Case/set_Camel_Case.
 * Declare the prototype for these functions in TimeWp.h. 
 * ----------------------------------------------------------------- @endcond */

/**@cond ***********************************************************************
 * TODO: Update all comment fields and delete this line.
 * MOD: For functions with project scope, remove 'static' keyword and declare
 * prototype in TimeWp.h
 * -------------------------------------------------------------------- @endcond
 * @fn          return_type function_name(TYPE parameter1, TYPE parameter2)
 * @return      Describe the function's return value here.
 * @param       parameter1 Describe the function's 1st parameter here.
 * @param       parameter2 Describe the function's 2nd parameter here.
 * @author      avijayak
 * @date        Aug 18, 2012, 10:21:06 PM
 * @brief	Provide a brief description of the module here (Single line).
 * @details	Provide a detailed description of the module here (Multi-line).
 * @pre         Mention any preconditions required to use this function.
 * @post        Mention any postcondition required to use this function.
 * @bug         List any bugs in this module.
 * @note        List any odd modes while using this function.
 * @warning     List any warnings against using the objects of this function.
 *******************************************************************************
 */

Std_ReturnType
TimeWp_GetTime( struct tm* timeBuf )
{
  return( PalDs1629_GetTime( (PalDs1629_BreakdownTimeType*)timeBuf ) );
}

/** End of TimeWp_GetTime *********** Copyright (C) 2012 Afflatus Techtronics */


/**@cond ***********************************************************************
 * TODO: Update all comment fields and delete this line.
 * MOD: For functions with project scope, remove 'static' keyword and declare
 * prototype in TimeWp.h
 * -------------------------------------------------------------------- @endcond
 * @fn          return_type function_name(TYPE parameter1, TYPE parameter2)
 * @return      Describe the function's return value here.
 * @param       parameter1 Describe the function's 1st parameter here.
 * @param       parameter2 Describe the function's 2nd parameter here.
 * @author      avijayak
 * @date        Aug 26, 2012, 9:32:38 AM
 * @brief	Provide a brief description of the module here (Single line).
 * @details	Provide a detailed description of the module here (Multi-line).
 * @pre         Mention any preconditions required to use this function.
 * @post        Mention any postcondition required to use this function.
 * @bug         List any bugs in this module.
 * @note        List any odd modes while using this function.
 * @warning     List any warnings against using the objects of this function.
 *******************************************************************************
 */

Std_ReturnType
TimeWp_GetDate( struct tm* timeBuf )
{
  return( PalDs1629_GetDate( (PalDs1629_BreakdownTimeType*)timeBuf ) );
}

/** End of function_name ************ Copyright (C) 2012 Afflatus Techtronics */


/**@cond ***********************************************************************
 * TODO: Update all comment fields and delete this line.
 * MOD: For functions with project scope, remove 'static' keyword and declare
 * prototype in TimeWp.h
 * -------------------------------------------------------------------- @endcond
 * @fn          return_type function_name(TYPE parameter1, TYPE parameter2)
 * @return      Describe the function's return value here.
 * @param       parameter1 Describe the function's 1st parameter here.
 * @param       parameter2 Describe the function's 2nd parameter here.
 * @author      avijayak
 * @date        Aug 18, 2012, 10:42:31 PM
 * @brief	Provide a brief description of the module here (Single line).
 * @details	Provide a detailed description of the module here (Multi-line).
 * @pre         Mention any preconditions required to use this function.
 * @post        Mention any postcondition required to use this function.
 * @bug         List any bugs in this module.
 * @note        List any odd modes while using this function.
 * @warning     List any warnings against using the objects of this function.
 *******************************************************************************
 */

Std_ReturnType
TimeWp_SetTime( struct tm* timeBuf, PalDs1629_AmPmType ampm )
{
  Std_ReturnType retVal = E_NOT_OK;

  set_PalDs1629_AmPm( ampm );
  retVal = PalDs1629_SetHourMode( _12_HOUR );
  retVal &= PalDs1629_SetTime( (PalDs1629_BreakdownTimeType*)timeBuf );

  return( retVal );
}

/** End of TimeWp_SetTime *********** Copyright (C) 2012 Afflatus Techtronics */

/**@cond ***********************************************************************
 * TODO: Update all comment fields and delete this line.
 * MOD: For functions with project scope, remove 'static' keyword and declare
 * prototype in TimeWp.h
 * -------------------------------------------------------------------- @endcond
 * @fn          return_type function_name(TYPE parameter1, TYPE parameter2)
 * @return      Describe the function's return value here.
 * @param       parameter1 Describe the function's 1st parameter here.
 * @param       parameter2 Describe the function's 2nd parameter here.
 * @author      avijayak
 * @date        Aug 19, 2012, 12:06:31 AM
 * @brief	Provide a brief description of the module here (Single line).
 * @details	Provide a detailed description of the module here (Multi-line).
 * @pre         Mention any preconditions required to use this function.
 * @post        Mention any postcondition required to use this function.
 * @bug         List any bugs in this module.
 * @note        List any odd modes while using this function.
 * @warning     List any warnings against using the objects of this function.
 *******************************************************************************
 */

Std_ReturnType
TimeWp_SetDate( struct tm* timeBuf )
{
  return( PalDs1629_SetDate( (PalDs1629_BreakdownTimeType*)timeBuf ) );
}

/** End of TimeWp_SetDate *********** Copyright (C) 2012 Afflatus Techtronics */

/* Get time parameters from UART and set it in DS1629 */
Std_ReturnType
TimeWp_SetTimeUart( void )
{
  Std_ReturnType retVal = E_OK;
  PalDs1629_AmPmType ampm = AM;
  struct tm timeBuf;
  char RxBuffer[12];
  char* pEnd;

  PrintWp_UiMsgRom( "\n\rEnter time. Syntax: hh mm ss <00/01> | ex.12 40 32 01 for 12:40:32 PM\n\r", PRINT_DEVICE_ID_UART0 );
  McalUart_Gets( (volatile uint8_t*)RxBuffer, 11, TRUE );

  timeBuf.tm_hour = strtol( (const char*)RxBuffer, &pEnd, 10 );
  timeBuf.tm_min = strtol( pEnd, &pEnd, 10 );
  timeBuf.tm_sec = strtol( pEnd, &pEnd, 10 );
  ampm = strtol( pEnd, (char**)NULL, 10 );

  retVal = TimeWp_SetTime( &timeBuf, ampm );

  return( retVal );
}

/* Get date parameters from UART and set it in DS1629 */
Std_ReturnType
TimeWp_SetDateUart( void )
{
  Std_ReturnType retVal = E_OK;
  PalDs1629_AmPmType ampm = AM;
  struct tm timeBuf;
  char RxBuffer[14];
  char* pEnd;

  PrintWp_UiMsgRom( "\n\rEnter date. Syntax: YYYY MM DD WW\n\r", PRINT_DEVICE_ID_UART0 );
  McalUart_Gets( (volatile uint8_t*)RxBuffer, 13, TRUE );

  timeBuf.tm_year = ( strtol( (const char*)RxBuffer, &pEnd, 10 ) - 1900 );
  timeBuf.tm_mon = strtol( pEnd, &pEnd, 10 );
  timeBuf.tm_mday = strtol( pEnd, &pEnd, 10 );
  timeBuf.tm_wday = strtol( pEnd, (char**)NULL, 10 );

  retVal = TimeWp_SetDate( &timeBuf );

  return( retVal );
}

#if 0
/* Continue this later,
 * see http://www.cs.cmu.edu/~guna/15-123S11/Lectures/Lecture14.pdf  */
/* Print time on UI display */
Std_ReturnType
TimeWp_PrintTime( Std_ReturnType(*pUi_Putc)(void*, char),
    void* funcParam1, void* funcParam2, struct tm* timeBuf )
{
  Std_ReturnType retVal = E_OK;
  char breakdownAscTime[26];
  uint8_t i;

  retVal = TimeWp_GetTime( timeBuf );
  asctime_r( (const struct tm *)timeBuf, breakdownAscTime );

  /* Point to hh in the breakdownAscTime array */
  breakdownAscTime += 11;

  if( E_OK == retVal )
    {
      for( i = 0; i < 8; i++ )
        {
          retVal = pUi_Putc( funcParam1, *breakdownAscTime++ );
        }
    }
  else
    {
      PrintWp_UiMsgRom( "\n\rFailed to get time.\n\r", PRINT_DEVICE_ID_UART0 );
    }

  return( retVal );
}
#endif

/** END OF FILE ********************* Copyright (C) 2012 Afflatus Techtronics */
